package com.banciyuan.bcywebview.biz.search;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by squall on 2015/9/7.
 */
public class SearchAutoAdapter extends BaseAdapter {

    private List<String> slist = new ArrayList<String>();

    private LayoutInflater inflater;

    private Context mContext;

    private String orign;

    public SearchAutoAdapter(List<String> slist, Context mContext, String orign) {
        this.slist = slist;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
        this.orign = orign;
    }

    @Override
    public int getCount() {
        return slist.size();
    }

    @Override
    public Object getItem(int i) {
        return slist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    private class ViewHolder {
        private TextView textView;
        private ImageView magnifying;
        private ImageView gonext;
        private View bline;

        public ViewHolder(View convertView) {
            textView = (TextView) convertView.findViewById(R.id.search_auto_title);
            magnifying = (ImageView) convertView.findViewById(R.id.search_auto_magnifying);
            gonext = (ImageView) convertView.findViewById(R.id.search_auto_gonext);
            bline = convertView.findViewById(R.id.auto_line);
        }
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.search_auto_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        String s = slist.get(i);
        if (i < getCount() - 2) {
            viewHolder.magnifying.setVisibility(View.GONE);
            viewHolder.gonext.setVisibility(View.GONE);
            String text = String.format(mContext.getString(R.string.pink_format), orign);
            try {
                s = s.replaceAll(orign, text);
            } catch (Exception e) {
                e.printStackTrace();
            }
            viewHolder.bline.setVisibility(View.GONE);
        } else if (i == getCount() - 2) {
            s = String.format(mContext.getString(R.string.search_user_pink_format), orign);
            viewHolder.magnifying.setVisibility(View.VISIBLE);
            viewHolder.gonext.setVisibility(View.VISIBLE);
            viewHolder.bline.setVisibility(View.GONE);
        } else if (i == getCount() - 1) {
            s = String.format(mContext.getString(R.string.search_topic_pink_format),orign);
            viewHolder.magnifying.setVisibility(View.VISIBLE);
            viewHolder.gonext.setVisibility(View.VISIBLE);
            viewHolder.bline.setVisibility(View.VISIBLE);
        }
        viewHolder.textView.setText(Html.fromHtml(s));

        return view;
    }

    public void setOrign(String orign) {
        this.orign = orign;
    }
}
